#include "validationdialog.h"
#include "ui_validationdialog.h"

#include <QDebug>

ValidationListModel::ValidationListModel(QObject *parent) :
    QAbstractListModel(parent)
{

}

int ValidationListModel::rowCount(const QModelIndex &parent) const {
    if(parent.isValid())
        return 0;
    return _errors.size();
}

QVariant ValidationListModel::data(const QModelIndex &index, int role) const {
    if(!index.isValid() || role != Qt::DisplayRole)
        return QVariant();
    return _errors.at(index.row());
}

void ValidationListModel::setSource(const QSet<QString>& errors) {
    if(_errors.size() > 0) {
        beginRemoveRows(QModelIndex(),0, _errors.size() -1);
        _errors.clear();
        endRemoveRows();
    }
    if(errors.size() > 0) {
        beginInsertRows(QModelIndex(), 0, errors.size()-1);
        _errors = errors.toList();
        endInsertRows();
    }
}

ValidationDialog::ValidationDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ValidationDialog)
{
    ui->setupUi(this);
    ui->listView->setModel(&model);
}

ValidationDialog::~ValidationDialog()
{
    delete ui;
}

void ValidationDialog::setSource(const QSet<QString> &errors) {
    model.setSource(errors);
}

void ValidationDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
